package o2o.web.actions.area;

import com.alibaba.fastjson.JSON;
import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.JYPageUtils;
import com.jinyou.utils.common.ValidateUtil;
import o2o.area.ProvinceInfo;
import o2o.service.area.ProvinceService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author qilonghe【1061268464@qq.com】
 * @ClassName ProvinceController
 * @Description:
 * @date 2020/9/17 0017 上午 10:01
 * @Version 1.0
 */
@RestController
public class ProvinceController {
    @Autowired
    ProvinceService provinceService;

    // 分页获取所有满足条件的省
    @RequestMapping(method = RequestMethod.POST, value = "admin/province/listForPage")
    public String listForPage(Integer page, Integer size, String params) {
        Long totalCount = 0l;
        List<ProvinceInfo> reList = null;

        if (null == page) {
            page = 1;
        }
        if (null == size) {
            size = 10;
        }
        //数据
        reList = provinceService.list(params);

        if (ValidateUtil.isAbsList(reList)) {
            JYPageUtils.PageList pageList = JYPageUtils.getList(reList, page, size);
            if (null != pageList && ValidateUtil.isAbsList(pageList.list)) {
                reList = (List<ProvinceInfo>) pageList.list;
                totalCount = Long.parseLong(pageList.totalCount + "");

                return AjaxResult.successResult(reList, totalCount);
            }
        }

        return AjaxResult.successResult(reList, 0);
    }

    // 获取所有满足条件的省
    @RequestMapping(method = RequestMethod.POST, value = "admin/province/list")
    public String list(String params) {
        List<ProvinceInfo> list = provinceService.list(params);
        return AjaxResult.successResult(list);
    }

    // 添加
    @RequestMapping(method = RequestMethod.POST, value = "admin/province/add")
    public String add(ProvinceInfo province) {

        try {
            provinceService.add(province, false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult("添加失败,请稍后重试");
        }
        return AjaxResult.successResult();
    }

    // 批量添加
    @RequestMapping(method = RequestMethod.POST, value = "admin/province/addList")
    public String addList(String addJson) {
        if (StringUtils.isBlank(addJson)) {
            return AjaxResult.errorResult("请求参数不能为空");
        }
        List<ProvinceInfo> provinceList = JSON.parseArray(addJson, ProvinceInfo.class);
        if (provinceList != null) {
            for (ProvinceInfo province : provinceList) {
                try {
                    province.setId(null);
                    provinceService.add(province, true);
                } catch (Exception e) {
                    e.printStackTrace();
                    return AjaxResult.errorResult("添加失败,请稍后重试");
                }
            }
            return AjaxResult.successResult();
        }
        return AjaxResult.errorResult("参数不能为空");
    }

    // 更新
    @RequestMapping(method = RequestMethod.POST, value = "admin/province/modify")
    public String modify(ProvinceInfo province) {
        if (province.getId() == null) {
            return AjaxResult.errorResult("请求参数不能为空");
        }
        try {
            provinceService.modify(province);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult("更新失败,请稍后重试");
        }
        return AjaxResult.successResult();
    }

    // 删除
    @RequestMapping(method = RequestMethod.POST, value = "admin/province/delete")
    public String delete(Long id) {
        if (id == null) {
            return AjaxResult.errorResult("请求参数不能为空");
        }
        try {
            provinceService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult("删除失败,请稍后重试");
        }
        return AjaxResult.successResult();
    }
}